import React, { Component } from "react";
// Redirect用于做重定向, 根Route并列的
import { Link, Route, Switch, Redirect } from "react-router-dom";

const Home1 = () => {
  return <div>home1页面</div>;
};
const Home2 = () => {
  return <div>home2页面</div>;
};

class Home extends Component {
  render() {
    // 当一个组件作为路由渲染出来的时候，那么这个组件的props里面就能拿到对应的路由信息(对象)
    // console.log(this.props.match.path);
    const {
      match: { path },
    } = this.props;

    console.log(path);
    return (
      <div>
        <ul>
          <li>
            <Link to={`${path}/home1`}>home1</Link>
          </li>
          <li>
            <Link to={`${path}/home2`}>home2</Link>
          </li>
        </ul>

        <hr />

        <Switch>
          <Redirect from={path} to={`${path}/home1`} exact></Redirect>
          <Route path={`${path}/home1`} component={Home1}></Route>
          <Route path={`${path}/home2`} component={Home2}></Route>
        </Switch>
      </div>
    );
  }
}
class About extends Component {
  render() {
    return <div>关于页</div>;
  }
}
class Mine extends Component {
  render() {
    return <div>我的页</div>;
  }
}

class Basic extends Component {
  state = {};
  render() {
    return (
      <>
        <h3>路由重定向</h3>
        <ul>
          <li>
            <Link to="/home">home</Link>
          </li>
          <li>
            <Link to="/about">about</Link>
          </li>
          <li>
            <Link to="/mine">mine</Link>
          </li>
        </ul>

        <Switch>
          {/* Redirect在有Switch的情况下，需要加上exact */}
          <Redirect from="/" to="/home" exact></Redirect>
          <Route path="/home" component={Home}></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/mine" component={Mine}></Route>
        </Switch>
      </>
    );
  }
}

export default Basic;
